package com.bw.mallshop.mvvm.model;

import com.bw.common.BaseApplication;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.model.BaseModel;
import com.bw.mallshop.App;
import com.bw.mallshop.dao.MaybeFunction;
import com.bw.mallshop.entity.UserEntityValues;

import java.util.List;

import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

public class UserModel extends BaseModel {
    //获取本地room中用户数据->
    public Observable<BaseEntity>loadUser(){
        App app = (App) BaseApplication.getInstance();
        //由于maybe观察者为maybeObserver所以在m层与vm层关联时
        //无法直接复用 baseviewmodel中封装得Observer
        //将maybe转化为Observable
        Maybe<List<UserEntityValues>> maybe = app.db.userDao().queryUserList();
        //通过操作符将maybe进行转化
        Observable<BaseEntity> observable =
                maybe.flattenAsObservable(new MaybeFunction());
        return observable;
    }
}
